package com.rc.saas.tenant.service.battery;

import com.alibaba.fastjson.JSON;
import com.rc.saas.tenant.mapper.battery.BatterySimpleReportDetailsMapper;
import com.rc.saas.tenant.model.battery.BatteryOrderSimpleNotify;
import com.rc.saas.tenant.model.battery.BatteryOrderSimpleNotifyExample;
import com.rc.saas.tenant.model.battery.BatterySimpleReportDetails;
import com.rc.saas.tenant.model.battery.BatterySimpleReportDetailsExample;
import com.rc.saas.tenant.vo.battery.BatterySimpleReportDetailsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BatterySimpleReportDetailsServiceImpl implements BatterySimpleReportDetailsService {
    @Autowired
    private BatterySimpleReportDetailsMapper batterySimpleReportDetailsMapper;

    @Autowired
    private BatteryOrderSimpleNotifyService batteryOrderSimpleNotifyService;

    @Override
    public BatterySimpleReportDetailsMapper getMapper() {
        return batterySimpleReportDetailsMapper;
    }

    @Override
    public BatterySimpleReportDetailsVo listBatterySimpleReportDetailsVoByOutOrderNum(String outOrderNum) {
        BatterySimpleReportDetailsExample example = new BatterySimpleReportDetailsExample();
        example.createCriteria().andOutOrderNumEqualTo(outOrderNum);
        List<BatterySimpleReportDetails> batterySimpleReportDetails = batterySimpleReportDetailsMapper.selectByExample(example);
        //po转vo
        List<BatterySimpleReportDetailsVo> list = JSON.parseArray(JSON.toJSONString(batterySimpleReportDetails), BatterySimpleReportDetailsVo.class);
        //结果主干
        BatterySimpleReportDetailsVo result = new BatterySimpleReportDetailsVo();
        List<BatterySimpleReportDetailsVo> model=new ArrayList<>();
        result = list.stream().filter(batterySimpleReportDetailsVo -> "0".equals(batterySimpleReportDetailsVo.getDetailType().toString()))
                .findFirst().orElseGet(() -> null);

        //模块排序
        BatterySimpleReportDetailsVo model1=listProjecByModel(list,"电池功能表现");
        if(model1!=null){
            model.add(model1);
        }
        BatterySimpleReportDetailsVo model2=listProjecByModel(list,"电池安全性能");
        if(model2!=null){
            model.add(model2);
        }
        BatterySimpleReportDetailsVo model3=listProjecByModel(list,"电池损伤检测");
        if(model3!=null){
            model.add(model3);
        }
        BatterySimpleReportDetailsVo model4=listProjecByModel(list,"电池整体表现");
        if(model4!=null){
            model.add(model4);
        }

        result.setModel(model);

        BatteryOrderSimpleNotifyExample example1=new BatteryOrderSimpleNotifyExample();
        example1.createCriteria().andOutOrderNumEqualTo(outOrderNum);
        BatteryOrderSimpleNotify batteryOrderSimpleNotify=
                batteryOrderSimpleNotifyService.selectByExample(example1).stream().findFirst().orElseGet(()->null);
        if(batteryOrderSimpleNotify!=null){
            result.setCarNo(batteryOrderSimpleNotify.getCarNum());
            result.setCarBrand(batteryOrderSimpleNotify.getCarType());
            result.setVin(batteryOrderSimpleNotify.getCarVin());
        }

        return result;
    }

    public BatterySimpleReportDetailsVo  listProjecByModel(List<BatterySimpleReportDetailsVo> list, String moduleName) {
        BatterySimpleReportDetailsVo model =
                list.stream().filter(b1 -> "1".equals(b1.getDetailType().toString())&&moduleName.equals(b1.getModuleName())).findFirst().orElseGet(() -> null);
        if(model!=null){
            List<BatterySimpleReportDetailsVo> projectList=
                    list.stream().filter(b2 -> "2".equals(b2.getDetailType().toString())&&moduleName.equals(b2.getModuleName())).collect(Collectors.toList());
            if(projectList.size()>0){
                model.setProject(projectList);
            }
        }

        return model;
    }
}
